﻿using System;
using System.Windows.Forms;

namespace Amgine
{
    partial class DictionaryEditForm : Form
    {
        private Dictionary dictionary;

        public DictionaryEditForm(Dictionary dictionary)
        {
            InitializeComponent();
            this.dictionary = (dictionary == null ? new Dictionary() : dictionary);
        }

        private void button_save_Click(object sender, EventArgs e)
        {
            dictionary.Code = textBox_code.Text;
            dictionary.Info = textBox_info.Text;
            dictionary.Type = comboBox_type.SelectedIndex;
            dictionary.IsEncrypted = (comboBox_flag.SelectedIndex == 0);

            if (validateData())
            {
                if (dictionary.createOrUpdate())
                {
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    DialogResult = DialogResult.Retry;
                }
                Close();
            }
        }

        private bool validateData()
        {
            if (string.IsNullOrWhiteSpace(dictionary.Code))
            {
                MessageBox.Show("值不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrWhiteSpace(dictionary.Info))
            {
                MessageBox.Show("名称不可为空", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }

        private void button_cancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private void DictionaryEditForm_Load(object sender, EventArgs e)
        {
            textBox_code.Text = this.dictionary.Code;
            textBox_info.Text = this.dictionary.Info;
            comboBox_type.SelectedIndex = this.dictionary.Type;
            comboBox_flag.SelectedIndex = (this.dictionary.IsEncrypted ? 0 : 1);
        }
    }
}
